package com.ouyy.volunteerservice.volunteer.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ouyy.volunteerservice.volunteer.domain.po.InfoNotice;
import com.ouyy.volunteerservice.volunteer.domain.po.volunteerView;
import com.ouyy.volunteerservice.volunteer.service.InfoNoticeService;
import com.ouyy.volunteerservice.volunteer.utils.RedisUtil;
import com.ouyy.volunteerservice.volunteer.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/infotice")
public class InfoNoticeController {

    @Autowired
    private InfoNoticeService infoNoticeService;

    @Autowired
    private RedisUtil redisUtil;

    // 查询所有公告
    @GetMapping("/all")
    public ResponseEntity<List<InfoNotice>> getAllInfoNotices() {
        redisUtil.setValue("test","111",300);
        List<InfoNotice> list = infoNoticeService.list();
        return ResponseEntity.ok(list);
    }

    @PostMapping("/search")
    public ResponseEntity<List<InfoNotice>> searchByTitle(@RequestBody Map<String,String> titleMap) {
        String title = titleMap.get("title");
        QueryWrapper<InfoNotice> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", title);
        List<InfoNotice> results = infoNoticeService.list(queryWrapper);

        if (!results.isEmpty()) {
            return ResponseEntity.ok(results);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 根据 ID 查询公告
    @GetMapping("/{id}")
    public ResponseEntity<InfoNotice> getInfoNoticeById(@PathVariable("id") Integer id) {
        //在点击详情前新增点击数
        infoNoticeService.incrementClickCount(id);
        InfoNotice infoNotice = infoNoticeService.getById(id);
        if (infoNotice != null) {
            return ResponseEntity.ok(infoNotice);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 新增公告
    @PostMapping("/")
    public ResponseEntity<InfoNotice> createInfoNotice(@RequestBody InfoNotice infoNoticePO) {
        boolean saved = infoNoticeService.save(infoNoticePO);
        if (saved) {
            return ResponseEntity.ok(infoNoticePO);
        } else {
            return ResponseEntity.badRequest().build();
        }
    }

    // 修改公告
    @PutMapping("/{id}")
    public ResponseEntity<InfoNotice> updateInfoNotice(@PathVariable("id") Integer id, @RequestBody InfoNotice infoNoticePO) {
        infoNoticePO.setId(id);
        boolean updated = infoNoticeService.updateById(infoNoticePO);
        if (updated) {
            return ResponseEntity.ok(infoNoticePO);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    // 删除公告
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteInfoNotice(@PathVariable("id") Integer id) {
        boolean deleted = infoNoticeService.removeById(id);
        if (deleted) {
            return ResponseEntity.noContent().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/count")
    public ResponseResult countInfoNotices() {
        long count = infoNoticeService.count();
        return new ResponseResult(true, "查询成功",count);
    }

}
